import { Controller, Get, Post, Body, Patch, Param, Delete, Req, UseInterceptors, HttpException, UseFilters, HttpCode, HttpStatus, UseGuards, Query } from '@nestjs/common';
import { UserService } from './user.service';
import { RegistUserDto } from './dto/regist-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { LoginUserDto } from './dto/login-user.dto';
//引入拦截器
import { ResponseInterceptor } from 'src/response/response.interceptor';
import { ErrorfilterFilter } from 'src/errorfilter/errorfilter.filter';

//引入管道
import { ValidatorpiePipe } from 'src/validatorpie/validatorpie.pipe';
import { AuthGuard } from 'src/auth/auth.guard';

//自定义装饰器
import { IsPublic } from 'src/is-public/is-public.decorator';

@Controller({
  path: 'user',
  version: '1'
})
//设置局部拦截器
@UseInterceptors(ResponseInterceptor)
//设置局部过滤器
@UseFilters(ErrorfilterFilter)
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Post('regist')
  @IsPublic()
  //设置返回的状态码
  @HttpCode(HttpStatus.OK)
  create(@Body(ValidatorpiePipe) createUserDto: RegistUserDto, @Req() req) {
    return this.userService.regist(createUserDto);
  }

  @IsPublic()
  @Post('login')
  login(@Body(ValidatorpiePipe) loginUserDto: LoginUserDto) {
    return this.userService.login(loginUserDto);
  }

  @Get(':id')
  // @UseGuards(AuthGuard)
  findOne(@Param('id') id: string, @Req() req) {
    // const userId = req.session.userId
    return this.userService.getuser(id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.updateuser(id, updateUserDto);
  }

  //关注
  @Get('subscribe/:id')
  addSub(@Param('id') following: string, @Req() req) {
    const follower = req.session.userId
    return this.userService.addSub(follower, following)
  }
  
  //取消关注
  @Get('dissubscribe/:id')
  @IsPublic()
  subtractSub(@Param('id') following: string, @Req() req) {
    const follower = req.session.userId
    return this.userService.subtractSub(follower, following)
  }


  @Get('sublist/list')
  Sublist(@Req() req, @Query() query) {
    const { page = 1, pagesize = 10 } = query
    const follower = req.session.userId
    return this.userService.Sublist(follower,page,pagesize)
  }

  //获取粉丝数

  @Get('getfans/list')
  async fansget(@Req() req, @Query() query){
    const { page = 1, pagesize = 10 } = query
    const following = req.session.userId
    return await this.userService.Fansget(following,page,pagesize)
  }
}
